<?php
/**
 *会员模型
 *yangbin
 */

if(!defined('APP_HOME')) exit('Access Denied');

class memberMod extends lib_Model
{
    protected $tbl = 'member'; #当前模型操作的主表
  	protected $addressTbl = 'member_address';#要连接的用户组表名
	protected $key = array('id'=>'member_address.member_id');#当前模型要连接的表与对应外键
    protected $ckName = array(); #COOKIE中的各键值名
    
    /*
    #初始化
    */
    public function _init( )
    {
    	$this->ckName = array(
    		'member_uid' =>'member_uid',#
    		'member_name'=>'member_name',#
    		'member_ltime'=>'member_ltime',#
    		'member_ntime'=>'member_ntime',#
    		'member_lip'=>'member_lip',#
    		'member_key'=>'member_key',#
    		'member_sig'=>'member_sig'#
    	);
    }

    /*
    #检查用户是否登录
    */
    public function isLogin( )
    {
        if($_COOKIE[$this->ckName['member_uid']] == '') return false; #用户ID
        if($_COOKIE[$this->ckName['member_name']] == '') return false; #用户名
        if($_COOKIE[$this->ckName['member_ltime']] == '') return false; #用户上次登录时间
        if($_COOKIE[$this->ckName['member_ntime']] == '') return false; #用户本次登录时间
        if($_COOKIE[$this->ckName['member_lip']] == '') return false; #用户上次登录IP
        if($_COOKIE[$this->ckName['member_sig']] == '') return false; #用户本次登录签名
        #所有的COOKIE项与密钥串起来MD5出一个签名
        $sig = COOK_KEY;
        $sig .= $_COOKIE[$this->ckName['member_name']];
        $sig .= COOK_KEY;
        $sig .= $_COOKIE[$this->ckName['member_ltime']];
        $sig .= $_COOKIE[$this->ckName['member_ntime']];
        $sig .= COOK_KEY;
        $sig .= $_COOKIE[$this->ckName['member_uid']];
        $sig .= $_COOKIE[$this->ckName['member_lip']];
        $sig = md5(md5($sig) . COOK_KEY);
        print_r($sig);
        if($sig == $_COOKIE[$this->ckName['member_sig']]) return true;
        return false; #异常
    }

    /*
    #登录
    */
    public function login( $name, $passwd, $expire_time = false )
    {
        if($name == '' || $passwd == '') return 0; #缺少参数
        //$userConfig = $this->loadConfig('user');#加载与用户相关的配置文件
        //通过正则匹配会员用email或者用户名方式登录
        if(preg_match('/^\w+[\w.-]*@\w+[.]{1}[\w.-]+[\w]+$/u', $name)){
            $user = $this->setEmail($name);
        }else{
            $user = $this->setName($name);
        }
        $user = $this->setLimit(1)->select('*');
        if(!$user) return '-1'; #用户名或email不存在
        if($user['password'] !== $this->createPasswd($passwd)) return '-2'; #密码不正确
        $ntm = $_SERVER['REQUEST_TIME'];
        $update = array();
        $update['logintime'] = $ntm;
        $update['loginip'] = getIp( );
        if(!$this->update($update)) return '-3'; #用户登录信息更新失败
        #注册COOKIE
        $expire_time = $expire_time ? 86400 : 0;
        #计算出登录签名
        $sig = COOK_KEY;
        $sig .= $user['username'];
        $sig .= COOK_KEY;
        $sig .= $user['logintime'];
        $sig .= $ntm;
        $sig .= COOK_KEY;
        $sig .= $user['id'];
        $sig .= $user['loginip'];
        $sig = md5(md5($sig) . COOK_KEY);
        
        if(!cookie($this->ckName['member_uid'], $user['id'], $expire_time)) return '-3';
        if(!cookie($this->ckName['member_name'], $user['username'], $expire_time)) return '-3';
        if(!cookie($this->ckName['member_ltime'], $user['logintime'], $expire_time)) return '-3';
        if(!cookie($this->ckName['member_ntime'],$ntm,$expire_time)) return '-3';
        if(!cookie($this->ckName['member_lip'], $user['loginip'], $expire_time)) return '-3';
        if(!cookie($this->ckName['member_sig'], $sig, $expire_time)) return '-3';
        return true;
    }
    
    /*
    #退出
    */
    public function logout()
    {
		#把所有的COOKIE给清除掉
		$tm = 0 - 86400;
		if(!cookie($this->ckName['member_uid'], '', $tm)) return false;
		if(!cookie($this->ckName['member_name'], '', $tm)) return false;
		if(!cookie($this->ckName['member_ltime'], '', $tm)) return false;
		if(!cookie($this->ckName['member_ntime'], '', $tm)) return false;
		if(!cookie($this->ckName['member_lip'], '', $tm)) return false;
		if(!cookie($this->ckName['ck_ntime'], '', $tm)) return false;
		if(!cookie($this->ckName['member_sig'], '', $tm)) return false;
		return true;
    }    
    
	/*
    #获取当前用户的ID
    */
	public function getNowUid()
	{
		return abs(intval($_COOKIE[$this->ckName['member_uid']]));
	}

 	/*
    #获取指定ID的用户信息
    */
    public function getUser($id = 0, $sel = '*')
    {
        if(!$id) $id = $this->getNowUid();
        if(!$id) return null;
        if(is_array($id)) $id = join(',', $id);
        else $this->setLimit(1);
        $res = $this->setId($id)->setIsDefault(1)->joinTable('member_address')->select($sel);
        return $res;
    }
    
    public function setIsDefault($val){
    	 $this->setWhere($this->addressTbl.'.isdefault', $val);
    	 return $this;
    }

    /*
    #添加会员
    */
    public function addUser( $user )
    {
        return $this->insert($user);
    }
    
 	/*
    #修改会员信息
    */
    public function editUser($user, $id = 0)
    {
		if($id) $this->setId($id);
		elseif(!empty($user['id'])) $this->setId($user['id']);
		if(isset($user['id'])) unset($user['id']);

		return $this->update($user);
    }

	/*
    #删除会员信息
    */
    public function delMember($id)
    {
		return $this->setId($id)->delete();
    }
    
    /*
    #检查用户名是否存在
    */
    public function isName( $m )
    {
        $res = $this->setName($m)->select('id');
        if(null === $res)
            return 1; #不存在
        elseif($res)
            return count($res); #存在
        return false; #异常
    }

    /*
    #检查邮箱地址是否存在
    */
    public function isEmail( $m )
    {
        $res = $this->setEmail($m)->select('id');
        if(null === $res)
            return 1; #不存在
        elseif($res)
            return count($res); #存在
        return false; #异常
    }

	/*
	#设置用户ID
	*/
	public function setId($d, $c = 'is')
	{
		return $this->setWhere($this->tbl.'.id', $d, $c);
	}

    /*
	#设置用户名称
	*/
    public function setName( $d, $c = 'is' )
    {
        return $this->setWhere('username', $d, $c);
    }

    /*
	#设置用户EMAIL
	*/
    public function setEmail( $d, $c = 'is' )
    {
        return $this->setWhere('email', $d, $c);
    }

    /*
	#生产用户密码
	*/
    public function createPasswd( $d )
    {
        return md5(md5(strrev($d)) . $d);
    }
}


